<template>
    <div>

        <div class="container">
            <!-- <div class="handle-box"> -->
            <el-form :model="queryParams" ref="queryForm" :inline="true">
                <el-form-item label="属地" prop="deptName">
                    <el-input
                            v-model="queryParams.deptName"
                            placeholder="请输入属地"
                            clearable
                            size="small"
                            style="width: 240px"
                            @keyup.enter.native="handleQuery"
                    />
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-select
                            v-model="queryParams.status"
                            placeholder="部门状态"
                            clearable
                            size="small"
                            style="width: 240px"
                    >
                        <el-option
                                v-for="dict in catOptions"
                                :key="dict.value"
                                :label="dict.label"
                                :value="dict.value"
                        />
                    </el-select>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" v-has-permi="['system:dept:list']"
                               @click="handleQuery">搜索
                    </el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
            <!-- </div> -->
            <el-row :gutter="10" class="mb8 handle-box">
                <el-col :span="1.5">
                    <el-button
                            type="primary"
                            plain
                            icon="el-icon-plus"
                            size="mini"
                            v-has-permi="['system:dept:add']"
                            @click="handleAdd"
                    >新增
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                            type="info"
                            plain
                            icon="el-icon-sort"
                            size="mini"
                            @click="toggleExpandAll"
                    >展开/折叠
                    </el-button>
                </el-col>
            </el-row>

            <el-table
                    border
                    v-if="refreshTable"
                    v-loading="loading"
                    :data="deptList"
                    row-key="deptId"
                    :default-expand-all="isExpandAll"
                    :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
            >
                <el-table-column prop="deptName" label="属地"></el-table-column>
                <el-table-column prop="sort" label="排序" align="center"></el-table-column>
                <el-table-column prop="status" label="状态" align="center">
                    <template slot-scope="scope">
                        <el-switch
                                v-model="scope.row.status"
                                active-value="0"
                                inactive-value="1"
                                disabled
                        ></el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" align="center" prop="createTime">
                    <template slot-scope="scope">
                        <span>{{ parseTime(scope.row.createTime) }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                style="color: #67C23A"
                                @click="handleUpdate(scope.row)"
                                v-has-permi="['system:dept:edit']"
                        >修改
                        </el-button>
                        <el-button
                                size="mini"
                                style="color: #409EFF"
                                @click="handleAdd(scope.row)"
                                v-has-permi="['system:dept:add']"
                        >新增
                        </el-button>
                        <el-button
                                v-if="scope.row.parentId != 0"
                                size="mini"
                                class="red"
                                @click="handleDelete(scope.row)"
                                v-has-permi="['system:dept:delete']"
                        >删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>

        </div>

        <!-- 编辑弹出框 -->
        <!-- <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="用户名">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="地址">
                    <el-input v-model="form.address"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog> -->

        <!-- 添加或修改社区对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="600" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                <el-row>
                    <el-col :span="24" v-if="form.parentId !== 0">
                        <el-form-item label="上级社区" prop="parentId">
                            <treeselect v-model="form.parentId" :options="deptOptions" :normalizer="normalizer"
                                        placeholder="选择上级社区"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="属地" prop="deptName">
                            <el-input v-model="form.deptName" placeholder="请输入属地"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="显示排序" prop="sort">
                            <el-input-number v-model="form.sort" controls-position="right" :min="0"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="负责人" prop="leader">
                            <el-input v-model="form.leader" placeholder="请输入负责人" maxlength="20"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="联系电话" prop="phone">
                            <el-input v-model="form.phone" placeholder="请输入联系电话" maxlength="11"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="邮箱" prop="email">
                            <el-input v-model="form.email" placeholder="请输入邮箱" maxlength="50"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="社区状态">
                            <el-radio-group v-model="form.status">
                                <el-radio
                                        v-for="dict in catOptions"
                                        :key="dict.value"
                                        :label="dict.value"
                                >{{dict.label}}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import request from '@/api/system/dept';
    import Treeselect from "@riophae/vue-treeselect";
    import "@riophae/vue-treeselect/dist/vue-treeselect.css";

    export default {
        name: 'basetable',
        components: {Treeselect},
        data() {
            return {
                // 是否展开，默认全部展开
                isExpandAll: true,
                // 重新渲染表格状态
                refreshTable: true,
                // 查询参数
                queryParams: {
                    deptName: undefined,
                    status: undefined
                },
                deptList: [],
                catOptions: [
                    {
                        value: '0',
                        label: '正常'
                    }, {
                        value: '1',
                        label: '停用'
                    }
                ],
                // editVisible: false,
                // addVisible: false,
                // 社区树选项
                deptOptions: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                form: {},
                // 表单校验
                rules: {
                    parentId: [
                        {required: true, message: "上级社区不能为空", trigger: "blur"}
                    ],
                    deptName: [
                        {required: true, message: "属地不能为空", trigger: "blur"}
                    ],
                    orderNum: [
                        {required: true, message: "显示排序不能为空", trigger: "blur"}
                    ],
                    email: [
                        {
                            type: "email",
                            message: "'请输入正确的邮箱地址",
                            trigger: ["blur", "change"]
                        }
                    ],
                    phone: [
                        {
                            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
                            message: "请输入正确的手机号码",
                            trigger: "blur"
                        }
                    ]
                }
            };
        },
        created() {
            this.getList();
        },
        methods: {
            getList() {
                this.loading = true;
                request.deptTreeList(this.queryParams).then(res => {
                    this.deptList = this.handleTree(res.data.list, "deptId");
                });
                this.loading = false;
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageIndex = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.dateRange = [];
                this.resetForm("queryForm");
                this.handleQuery();
            },
            /** 展开/折叠操作 */
            toggleExpandAll() {
                this.refreshTable = false;
                this.isExpandAll = !this.isExpandAll;
                this.$nextTick(() => {
                    this.refreshTable = true;
                });
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    "deptName": null,
                    "email": null,
                    "leader": null,
                    "parentId": null,
                    "phone": null,
                    "sort": null,
                    "status": '0'
                };
                this.resetForm("form");
            },

            /** 转换社区数据结构 */
            normalizer(node) {
                if (node.children && !node.children.length) {
                    delete node.children;
                }
                return {
                    id: node.deptId,
                    label: node.deptName,
                    children: node.children
                };
            },
            /** 新增按钮操作 */
            handleAdd(row) {
                this.reset();
                if (row != undefined) {
                    this.form.parentId = row.deptId;
                }
                this.open = true;
                this.title = "添加社区";
                request.deptTreeList(this.queryParams).then(response => {
                    this.deptOptions = this.handleTree(response.data.list, "deptId");
                });
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                request.deptInfo({'deptId': parseInt(row.deptId)}).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "修改社区";
                });
                request.deptTreeList({'excludeDeptId': row.deptId}).then(response => {
                    this.deptOptions = this.handleTree(response.data.list, "deptId");
                });
            },
            /** 提交按钮 */
            submitForm: function () {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.form.deptId != undefined) {
                            request.updateDept(this.form).then(response => {
                                this.$message.success('修改成功');
                                this.open = false;
                                this.getList();
                            });
                        } else {
                            request.addDept(this.form).then(response => {
                                this.$message.success('新增成功');
                                this.open = false;
                                this.getList();
                            });
                        }
                    }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                this.$confirm('是否确认删除名称为"' + row.deptName + '"的数据项？', "提示", {
                    type: 'warning'
                }).then(function () {
                    return request.deleteDept({'deptId': parseInt(row.deptId)});
                }).then(() => {
                    this.$message.success('删除成功');
                    this.getList();
                }).catch(() => {
                });
            }

        }
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }

    .table {
        width: 100%;
        font-size: 14px;
    }

    .red {
        color: #ff0000;
    }

    .mr10 {
        margin-right: 10px;
    }

    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }
</style>
